home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk15 / sct / sct.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  6KB  |  171 lines

  1. /*  SetColorTable - CLI invoked ColorTable fiddler.
  2.     Copyright 1988 aklevin.  All rights reserved.
  3.  
  4.     Manx: cc +L, ln -lc32
  5. */
  6.  
  7. #include <intuition/intuitionbase.h>
  8. #include <graphics/gfxbase.h>
  9. #include <stdio.h>
  10.  
  11. #define INTUITION_REV    33
  12. #define GRAPHICS_REV    33
  13.  
  14. struct IntuitionBase *IntuitionBase;
  15. struct GfxBase *GfxBase;
  16.  
  17. /*  Error severity values:  */
  18. #define EXIT_LOW    0
  19. #define EXIT_MED    10
  20. #define EXIT_HIGH    20
  21.  
  22. /*  L E A V E  - Prints the error message (if any),
  23.                  closes any open libraries, and exits.
  24. */
  25. void
  26. leave(string, command_name, exit_val)
  27. char *string, *command_name;
  28. int exit_val;
  29. {
  30.     if (strlen(string)) fprintf(stderr, "%s: %s\n", command_name, string);
  31.     if (GfxBase) CloseLibrary(GfxBase);
  32.     if (IntuitionBase) CloseLibrary(IntuitiËK*k•¥í5)%•á¥Ñ¡•á¥Ñ}م±¥í5)õ%½©•¹‘±•…Ù•¡m) */
  33.  
  34. /*  U S A G E  - Gives the program parameter sumary.  */
  35. void
  36. usage(command_name, exit_val)
  37. char *command_name;
  38. int exit_val;
  39. {
  40. fprintf(stderr, "SetColorTable - Copyright 1988 aklevin.  All rights reserved.\n");
  41. fprintf(stderr, "\nUsage:\n");
  42. fprintf(stderr, "\n%s [-t \"Screen Title\"] Xrgb Xrgb Xrgb...\n", command_name);
  43. fprintf(stderr, "    Displays the current ColorTable settings for the Workbench Screen,\n");
  44. fprintf(stderr, "    (or the screen whose title is \"Screen Title\" if `-t' option is used)\n");
  45. fprintf(stderr, "    then sets the ColorTable to the values given by Xrgb Xrgb Xrgb... .\n");
  46. fprintf(stderr, "\n%s <colorfile [-t \"Screen Title\"]\n", command_name);
  47. fprintf(stderr, "    As above, but RGB values are in file 'colorfile'.\n");
  48. fprintf(stderr, "\n    Use \" >NIL:\" to prevent color values from being displayed.\n");
  49. fprintf(stderr, "    Use \" >file\" to save color values for later restoration.\n");
  50. fprintf(stderr, "    Any redirection ( <file  >file or >NIL:) MUST follow command name.\n");
  51. fprintf(stderr, "\nX r g b <- Amount of Blue  in this color (0 to f).\n");
  52. fprintf(stderr, "| | +----- Amount of Green in this color (0 to f).\n");
  53. fprintf(stderr, "| +------- Amount of Red   in this color (0 to f).\n");
  54. fprintf(stderr, "+--------- 0=Use value, 1=Skip value, 2=Skip this and all further values.\n\n");
  55. leave("", "", exit_val);
  56. }    /* end usage() */
  57.  
  58. /*  S E T   C O L O R   T A B L E  - Accepts RGB values from either stdin
  59.                                      or the command line, and uses those
  60.                                      values to alter the ColorTable for
  61.                                      the named screen.
  62. */
  63.  
  64. /*  The largest ColorTable size.  */
  65. #define TABLE_SIZE    32
  66.  
  67. main(argc, argv)
  68. int argc;
  69. char *argv[];
  70. {
  71.  
  72. /*
  73.   Variables you should know:
  74.       from_cli- 0 if RGB values will come from stdin, 1 if from command line.
  75.     max_colors- Maximum number of colors for this screen.
  76.     num_colors- Counter for current number of colors.
  77.          title- Screen Title.
  78. */
  79. int argp=1, i, from_cli, max_colors=2, num_colors=0, scan_ok;
  80. char title[81];
  81. UWORD table[TABLE_SIZE], *tmp;
  82. struct Screen *screen;
  83. struct ViewPort *viewport=NULL;
  84.  
  85. /*  Set the default window title.  */
  86. strcpy (title, "Workbench Screen");
  87.  
  88. /*  Argument parsing.  */
  89. if (argc > 1) {
  90.     /*  Give usage if first argument is a '?'.  */
  91.     if (argv[argp][0] == '?') usage(argv[0], EXIT_LOW);
  92.     if (argv[argp][0] == '-') {
  93.         /*  Found an option.  Is it valid ('t' or 'T')?  */
  94.         if (tolower(argv[argp][1]) == 't') {
  95.             /*  Valid option, title is part of this argument.  */
  96.             if (strlen(argv[argp]) > 2) strcpy(title, &argv[argp++][2]);
  97.             else {
  98.                 /*  Valid option, title is next argument.  */
  99.                 if (argc > ++argp) strcpy(title, argv[argp++]);
  100.                 /*  No title given.  */
  101.                 else usage(argv[0], EXIT_MED);
  102.             }  /* end else */
  103.         }  /* end if (tolower */
  104.         else usage(argv[0], EXIT_MED);  /*  Wasn't a valid option.  */
  105.     }  /* end if (argv */
  106. }  /* end if (argc */
  107.  
  108. /*  If there are still command line arguments
  109.     to be parsed, then they must be RGB values.
  110. */
  111. from_cli = (argp < argc) ? 1 : 0;
  112.  
  113. /*  Open intuition and graphics libraries.  */
  114. IntuitionBase=(struct IntuitionBase *)
  115.     OpenLibrary("intuition.library", INTUITION_REV);
  116. if (IntuitionBase==NULL )
  117.     leave("Couldn't open intuition.library.", argv[0], EXIT_HIGH);
  118.  
  119. GfxBase=(struct GfxBase *)
  120.     OpenLibrary("graphics.library", GRAPHICS_REV);
  121. if (GfxBase==NULL)
  122.     leave("Couldn't open graphics.library.", argv[0], EXIT_HIGH);
  123.  
  124. /*  Find the screen with the desired title.  */
  125. for (screen=IntuitionBase->FirstScreen; screen; screen=screen->NextScreen)
  126.     if (strcmp(screen->Title, title)==0) {
  127.         viewport = &(screen->ViewPort);
  128.         break;
  129.     }
  130.  
  131. /*  Quit if the screen couldn't be found.  */
  132. if (viewport==NULL) leave("Couldn't find that screen.", argv[0], EXIT_MED);
  133.  
  134. /*  Find out the maximum number of colors available.  Many thanks to
  135.     Bryce Nesbitt and Eugene Mortimore for opening my eyes to this.  */
  136. for (i=(int)screen->BitMap.Depth; i>1 ; i--) max_colors*=2;
  137.  
  138. /*  Print the current values, before they get changed.  */
  139. for (i=0; i<max_colors; i++)
  140.     printf("%04x%c", GetRGB4(viewport->ColorMap, i),
  141.         ((i<max_colors-1) ? ' ' : '\n'));
  142.  
  143. /*  Scanf the RGB values into the array 'table'.
  144.     There are four conditions which break out of this loop:
  145.     1) Current entry to be altered >= max_colors.
  146.     2) EOF reached on stdin or illegal value entered.
  147.     3) RGB values were being read from argv, and there are no more.
  148.     4) RGB value >= 0x2000, meaning "no more entries should be altered".
  149. */
  150. while ((num_colors < max_colors) && ( ! from_cli || (argp < argc))) {
  151.     if (from_cli) scan_ok = sscanf(argv[argp++], "%04x", &tmp);
  152.     else scan_ok = scanf("%04x", &tmp);
  153. /*  If EOF was reached or input wasn't legal,
  154.        don't alter this entry, or following ones.  */
  155.     if (scan_ok == EOF || scan_ok == NULL) break;
  156. /*  If first digit is >= 2, don't alter this entry, or following ones.  */
  157.     if (tmp >= 0x2000) break;
  158. /*  If first digit is >= 1, don't alter this entry.  */
  159.     if (tmp >= 0x1000) tmp = GetRGB4(viewport->ColorMap, num_colors);
  160.     table[num_colors++] = tmp;
  161. }  /* end while */
  162.  
  163. /*  Alter the ColorTable.  */
  164. LoadRGB4(viewport, &table[0], num_colors);
  165.  
  166. /*  All done.  */
  167. leave("", "", EXIT_LOW);
  168.  
  169. }    /* end main() */
  170.  
  171.